package com.xiaolei.lib.Activity.bean;

import java.lang.reflect.Method;
import java.util.List;

import com.xiaolei.lib.Activity.IActivity;

import android.content.Intent;

/**
 * 界面返回回调的驱动
 * 
 * @author xiaolei
 */
public class OnActivityResultDriver
{
	/**
	 * 用以驱动注册的界面返回回调
	 * 
	 * @param requestCode
	 * @param resultCode
	 * @param data
	 * @param activity
	 */
	public void driver(int requestCode, int resultCode, Intent data, IActivity activity, List<Integer> x_l_resultCodes,
			List<Integer> x_l_requestCodes, List<Method> x_l_metheds)
	{
		for (int i = 0; i < x_l_requestCodes.size(); i++)
		{
			if (x_l_requestCodes.get(i) == requestCode && x_l_resultCodes.get(i) == resultCode)
			{
				Method method = x_l_metheds.get(i);
				Class<?>[] paramTypes = method.getParameterTypes();
				Object args[] = null;
				try
				{
					args = new Object[paramTypes.length];
					for (int j = 0; j < paramTypes.length; j++)
					{
						if (paramTypes[j].isInstance(data))
						{
							args[j] = data;
						}
					}
					method.invoke(activity, args);
				} catch (Exception e)
				{
					try
					{
						args = new Object[paramTypes.length];
						method.invoke(activity, args);
					} catch (Exception e1)
					{
						e1.printStackTrace();
					}
					e.printStackTrace();
				}
			}
		}
	}
}
